package com.mxw.leetcode.D1排序;


import java.util.Arrays;
import java.util.HashMap;

/**
 * @author miao
 */
public class Demo1 {

    static int[] temp;

    public static void sort(int[] nums) {
        // 先给辅助数组开辟内存空间
        temp = new int[nums.length];
        // 排序整个数组（原地修改）
        sort(nums, 0, nums.length - 1);
    }

    private static void sort(int[] nums, int left, int right) {
        if (left == right) {
            return;
        }

        int mid = left + (right - left) / 2;

        sort(nums, left, mid);
        sort(nums, mid + 1, right);

        merge(nums, left, mid, right);
    }

    private static void merge(int[] nums, int left, int mid, int right) {

        for (int i = left; i <= right; i++) {
            temp[i] = nums[i];
        }

        int i = left, j = mid+1;
        for (int index = left; index <= right; index++) {
            if (i==mid+1){
                nums[index]=temp[j++];
            }else if (j==right+1){
                nums[index]=temp[i++];
            }else if (temp[i]>temp[j]){
                nums[index]=temp[j++];
            }else {
                nums[index]=temp[i++];
            }
        }
    }

}
